package com.atguigu.springboot.controller;


import com.atguigu.springboot.lb.LoadBalancer;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @Author: 19shusheng.com
 * @Description:
 * @Date: Created in 22:07 2020/10/19
 * @Modified By:
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
public class OrderController {
    //ip和端口写死了只能请求单一节点，集群的情况下消费者只关注服务的名称
    //private static final String PAYMENT_URL = "http://localhost:8001";
    private static final String PAYMENT_URL = "http:// ";
    /**
     *
     * @Description: 注入bean的方式@Autowired,@Resource,       @Bean是实例化bean
     *
     * @auther: madonghui
     * @date: 22:14 2020/10/20
     * @param:
     * @return:
     *
     */
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancer loadBalancer;
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment){
        /**
         *
         * @Description: 三个参数，请求路径，请求参数，需要返回的实体
         *
         * @auther: madonghui
         * @date: 22:26 2020/10/20
         * @param: [payment]
         * @return: com.atguigu.springboot.entities.CommonResult<com.atguigu.springboot.entities.Payment>
         *
         */
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class);
    }

    @GetMapping("/payment/getPaymentById/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id")Long id){
        /**
         *
         * @Description: 三个参数，请求路径，请求参数，需要返回的实体
         *
         * @auther: madonghui
         * @date: 22:26 2020/10/20
         * @param: [payment]
         * @return: com.atguigu.springboot.entities.CommonResult<com.atguigu.springboot.entities.Payment>
         *
         */
        return restTemplate.getForObject(PAYMENT_URL+"/payment/getPaymentById/"+id,CommonResult.class);
    }

    @GetMapping("/payment/getPaymentById2/{id}")
    public CommonResult<Payment> getPaymentById2(@PathVariable("id")Long id){
        /**
         *
         * @Description: 三个参数，请求路径，请求参数，需要返回的实体
         *
         * @auther: madonghui
         * @date: 22:26 2020/10/20
         * @param: [payment]
         * @return: com.atguigu.springboot.entities.CommonResult<com.atguigu.springboot.entities.Payment>
         *
         */
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/getPaymentById/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()){
            return forEntity.getBody();
        }else {
            return new CommonResult<>(500,"响应错误");
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances != null || instances.size() <= 0){
            return "毛也没有,instances"+instances.size();
        }
        ServiceInstance instances1 = loadBalancer.instances(instances);
        URI uri = instances1.getUri();
        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }

    // ====================> zipkin+sleuth
    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances != null || instances.size() <= 0){
            return "毛也没有,instances"+instances.size();
        }
        ServiceInstance instances1 = loadBalancer.instances(instances);
        URI uri = instances1.getUri();
        String result = restTemplate.getForObject(uri+"/payment/zipkin/", String.class);
        return result;
    }
}
